$(function () {
  var num1 = 0;
  var num2 = 0;

//   传入 shopid 和 areaid
  function render(shopid, areaid) {
      $.ajax({
          url: 'http://mmb.ittun.com/api/getgsproduct',
          type: 'GET',
          data: {
              shopid: shopid,
              areaid: areaid
          },
          success: function (backData) {
              // console.log(backData);
              var productHtml = template('product', backData);
              $('#container').html(productHtml);
              $('.fd').hide();

          }
      })
  }
  render(num1, num2);

  var shopnum;
  $('.pinPai').click(function () {
      $(this).parent().siblings().children().children('i').attr('class', 'fa fa-sort-desc');

      if ($(this).children('i').hasClass('fa fa-sort-desc')) {

          $(this).children('i').attr('class', 'fa fa-sort-up');
          $('.fd').show();
      } else {

          $(this).children('i').attr('class', 'fa fa-sort-desc');
          $('.fd').hide();
      }

      $.ajax({
          url: 'http://mmb.ittun.com/api/getgsshop',
          type: 'GET',
          success: function (backData) {

              for (var i = 0; i < backData.result.length; i++) {
                  backData.result[i].shopnum = shopnum;
                  // console.log(backData.result[i]);
              }

              var productHtml = template('pinPai', backData);
              $('#xuanRan').html(productHtml);


              $('#xuanRan').on('click', '.shopId', function () {
                  // console.log($(this).data('id'));
                  num1 = $(this).data('id');

                  render($(this).data('id'), num2);
                  $('.fd').hide();
                  $('.fa-sort-up').attr('class', 'fa fa-sort-desc');


                  $('.pinPai').children('span').html($(this).children().children().html());

                  shopnum = $(this).index();


              })

          }
      })
  });

  var areaNum;
  $('.diQu').click(function () {
      $(this).parent().siblings().children().children('i').attr('class', 'fa fa-sort-desc');


      if ($(this).children('i').hasClass('fa-sort-desc')) {
          $(this).children('i').attr('class', 'fa fa-sort-up');
          $('.fd').show();

      } else {
          $(this).children('i').attr('class', 'fa fa-sort-desc');
          $('.fd').hide();
      }

      $.ajax({
          url: 'http://mmb.ittun.com/api/getgsshoparea',
          type: 'GET',
          success: function (backData) {
              for (var i = 0; i < backData.result.length; i++) {
                  backData.result[i].areaNum = areaNum;
                  // console.log(backData.result[i]);
              }

              var productHtml = template('diQu', backData);
              $('#xuanRan').html(productHtml);

              $('#xuanRan').on('click', '.areaId', function () {

                  num2 = $(this).data('id');

                  render(num1, $(this).data('id'));
                  $('.fd').hide();
                  $('.fa-sort-up').attr('class', 'fa fa-sort-desc');

                  $(this).children().children().next().attr('class', 'fa fa-check');

                  var str = $(this).children().children().html();
                  str = str.substring(0, 2);
                  $('.diQu').children('span').html(str);

                  areaNum = $(this).index();

              })
          }
      })

  });


   
//  ------ 搜索 ------
var isTrue = true;
$('.hd .search').click(function () {
    console.log("搜索。。。");    
    // evev.target.dataset.id;
    if (isTrue) {
    //动态添加 属性    
    $(".popsearch").attr("style","display: block");
    
    isTrue = false;
    } else{
        $(".popsearch").removeAttr("style","display: none");
        isTrue = true;
    }

})


  // // 返回上一级页面
  // $('.header .left').click(function () {
  //     window.history.go("-1");
  // })


})